//
//  CookieManager.m
//  iOSProjectExtension
//
//  Created by Apple on 2020/1/10.
//  Copyright © 2020 lidianchao. All rights reserved.
//

#import "CookieManager.h"
#import "NetworkHelper.h"
#import "UserDefaultsManager.h"
#import "Config.h"
@implementation CookieManager
/*
 同步cookie
 **/
+(void)syncCookie
{

    NSMutableArray* cookieDictionary = [UserDefaultsManager getCookieArray];
    if (!cookieDictionary.count) {
        cookieDictionary = [UserDefaultsManager getCookieArray];
    }
    [self saveCookie];
    [self syncAFSecurityPolicy];
    for (int i=0; i < cookieDictionary.count; i++)
    {
        NSMutableDictionary* cookieDictionary1 = [UserDefaultsManager getCookieProperties:[cookieDictionary objectAtIndex:i]];
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}
+(void)saveCookie
{
    NSMutableArray *cookieArray = [[NSMutableArray alloc] init];
    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [cookieArray addObject:cookie.name];
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
        [cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
        [cookieProperties setObject:[NetworkHelper domainWithHost:HOST] forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
        [cookieProperties setObject:[NSNumber numberWithLong:cookie.version] forKey:NSHTTPCookieVersion];
        NSString *date = [NSString stringWithFormat:@"%@", [[NSDate date] dateByAddingTimeInterval:2629743]];
        [cookieProperties setObject:date forKey:NSHTTPCookieExpires];
        [UserDefaultsManager saveCookieProperties:cookieProperties key:cookie.name];
    }
    [UserDefaultsManager saveCookieArray:cookieArray];
}
//保存apptoken，不与域名有关
+ (NSString *)getApptokenString {
    return [UserDefaultsManager getLocalStringWithKey:@"cookie.value_string_app"];
}
//证书绑定
+ (void)syncAFSecurityPolicy {
    
    
}
@end
